home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl
-
- # Find different lines.pl
- # This script finds lines that are not the same between two files
- #
- # 12/22/95 Version 1.0
- # This script is free, public domain, no strings attached.
- # Igor Livshits <igorl@uiuc.edu>
-
-
- $openToWrite= "> ";
- $macPathDelimiter= ":";
- $argumentCounter= 0;
-
-
- # Read in the arguments passed to us from the AppleScript agent
- #
- $workingDirectory= $ARGV[$argumentCounter++] . $macPathDelimiter;
- $firstFile= $ARGV[$argumentCounter++];
- $secondFile= $ARGV[$argumentCounter++];
- $linesAddedFile= $ARGV[$argumentCounter++];
- $linesRetainedFile= "Lines retained";
- $linesRemovedFile= "Lines removed";
-
-
- # Set up input and output files
- #
- open(firstFile, $workingDirectory . $firstFile) || die "Cannot open $firstFile: $!";
- open(secondFile, $workingDirectory . $secondFile) || die "Cannot open $secondFile: $!";
- open(linesRetainedFile, $openToWrite . $workingDirectory . $linesRetainedFile) || die "Cannot open $linesRetainedFile: $!";
- open(linesAddedFile, $openToWrite . $workingDirectory . $linesAddedFile) || die "Cannot open $linesAddedFile: $!";
- open(linesRemovedFile, $openToWrite . $workingDirectory . $linesRemovedFile) || die "Cannot open $linesRemovedFile: $!";
-
-
- # Find the differences -- assumes sorted file contents
- #
- $currentSecondFileLine= <secondFile>; # pre-fetch the next line of the second file
- while($currentFirstFileLine= <firstFile>)
- {
- while (!eof(secondFile))
- { # skip until second file catches up with us
- last if ($currentFirstFileLine le $currentSecondFileLine);
-
- print (linesAddedFile $currentSecondFileLine); # this line is only in the second file
- $currentSecondFileLine= <secondFile>; # fetch the next line of the second file
- }
-
- if ($currentFirstFileLine eq $currentSecondFileLine)
- {
- print (linesRetainedFile $currentFirstFileLine); # here's a line common to both files
- $currentSecondFileLine= <secondFile>; # fetch the next line of the second file
- }
- else
- {
- print (linesRemovedFile $currentFirstFileLine); # this line is only in the first file
- }
- }
-
- while ($currentSecondFileLine= <secondFile>)
- { # dump the residual unmatched lines
- print (linesAddedFile $currentSecondFileLine); # this line is only in the second file
- }
-
-
- # Clean up
- #
- close(firstFile);
- close(secondFile);
- close(linesRetainedFile);
- close(linesAddedFile);
- close(linesRemovedFile);
-